package cn.foolishbird.hamal.frame.config.shiro;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.Collections;

/**
 * 自定义域
 *
 * @author Eric
 * @version 1.0
 * @date 2019-06-23
 */
@Slf4j
public class StatelessRealm extends AuthorizingRealm {

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof StatelessToken;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        StatelessToken statelessToken = (StatelessToken) token;
//        if (statelessToken == null) {
//            logger.error("用户携带 token 信息不存在，登录验证失败");
//            throw new AuthenticationException("token 信息不存在，登录验证失败");
//        }
//        boolean avail = statelessToken.verify();
//        if (!avail) {
//            logger.error("用户携带 token 对应的信息不全:{}，登录验证失败", JSON.toJSONString(statelessToken));
//            throw new AuthenticationException("token 对应的信息不全，登录验证失败");
//        }
//
//        SysUserToken userToken = sysUserTokenService.obtainByUserId(statelessToken.getUserId());
//
//        String signToken = SecureUtil.md5(statelessToken.getRandomNum() + userToken.getSysToken());
        return new SimpleAuthenticationInfo(statelessToken.getPrincipal(), null, this.getName());
    }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addRoles(Collections.emptySet());
        info.addStringPermissions(Collections.emptySet());
        return info;
    }
}
